package a51qindian.com.comprehensive.activity;

import android.content.Intent;
import android.view.View;
import android.widget.EditText;

import com.li.utils.KeyboardUtil;
import com.li.widget.MultipleLayout;
import com.li.widget.TopNavigationBar;

import java.util.HashMap;
import java.util.Map;

import a51qindian.com.comprehensive.Const;
import a51qindian.com.comprehensive.R;
import a51qindian.com.comprehensive.base.QDBaseActivity;
import a51qindian.com.comprehensive.model.BaseModel;
import a51qindian.com.comprehensive.model.UserInfo;
import a51qindian.com.comprehensive.net.ObservableHelper;
import a51qindian.com.comprehensive.net.RetrofitCreator;
import a51qindian.com.comprehensive.net.SelectorObserver;
import a51qindian.com.comprehensive.util.SpUtil;
import butterknife.BindView;
import butterknife.OnClick;
import io.reactivex.annotations.NonNull;
import io.reactivex.disposables.Disposable;

/**
 * Author: Li
 * Date: 2017/9/19 0019
 * Description: 登录界面
 */

public class LoginActivity extends QDBaseActivity {


    @BindView(R.id.et_account)
    EditText mEtAccount;
    @BindView(R.id.et_pwd)
    EditText mEtPwd;
    @BindView(R.id.top_navigation)
    TopNavigationBar mTopNavigationBar;
    @BindView(R.id.layout_content)
    MultipleLayout mLayoutContent;


    private static final int REQUEST_CODE_RESET=0x01;
    @Override
    protected int getLayoutId() {
        return R.layout.activity_login;
    }


    /**
     * 登录
     * @param account   用户名
     * @param pwd   密码
     */
    private void login(String account, String pwd) {
        Map<String, String> map = new HashMap<>();
        map.put("username", account);
        map.put("password", pwd);

        mLayoutContent.onLoadingWithContent("正在登录");
        ObservableHelper.createObservable(RetrofitCreator.createService().login(map))
                .subscribe(new SelectorObserver<BaseModel<UserInfo>>() {
                    @Override
                    protected void onErrorCodeInfo(int code, String info) {
                        mLayoutContent.onSuccess();
                        showToast(info);
                    }

                    @Override
                    protected void onSuccess(BaseModel<UserInfo> userInfoBaseModel) {
                        mLayoutContent.onSuccess();
                        UserInfo userInfo = userInfoBaseModel.getData();
                        SpUtil.saveUserInfo(
                                LoginActivity.this,
                                userInfo.getUid(),
                                userInfo.getMoney(),
                                userInfo.getGrade(),
                                userInfo.getUsername(),
                                userInfo.getTotal_money(),
                                userInfo.getIsshop());
                        Intent intent = new Intent(LoginActivity.this, MainActivity.class);

                        //当用户等级为商铺 并且 商铺信息未完善时 跳往完善界面
                        if (SpUtil.getUserInfo(LoginActivity.this, SpUtil.USER_GRADE).equals(Const.TYPE_SHOP)
                                && SpUtil.getUserInfo(LoginActivity.this, SpUtil.USER_IS_SHOP).equals("0")) {
                            intent.setClass(LoginActivity.this, PerfectShopInfoActivity.class);
                            startActivity(intent);
                            finish();
                            return;
                        }
                        intent.putExtra(Const.USER_TYPE,
                                SpUtil.getUserInfo(LoginActivity.this, SpUtil.USER_GRADE));
                        startActivity(intent);
                        finish();
                    }

                    @Override
                    public void onSubscribe(@NonNull Disposable d) {
                        addDisposable(d);
                    }

                    @Override
                    public void onError(@NonNull Throwable e) {
                        mLayoutContent.onSuccess();
                        showToast(e.getMessage());
                    }
                });
    }

    @Override
    protected void initSth() {
        super.initSth();
        mTopNavigationBar.setNavigationBarListener(this);
    }


    @OnClick({R.id.btn_reset_pwd, R.id.btn_login})
    public void onViewClicked(View view) {
        switch (view.getId()) {
            case R.id.btn_reset_pwd:
                Intent intent=new Intent(LoginActivity.this,ResetPwdActivity.class);
                intent.putExtra("source","login");
                startActivityForResult(intent,REQUEST_CODE_RESET);
                break;
            case R.id.btn_login:
                KeyboardUtil.hideKeyboard(this, mLayoutContent);
                login(mEtAccount.getText().toString(), mEtPwd.getText().toString());
                break;
        }
    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        if (requestCode==REQUEST_CODE_RESET&&resultCode==RESULT_OK) {
            String account=data.getStringExtra("account");
            String pwd=data.getStringExtra("pwd");
            mEtAccount.setText(account);
            mEtPwd.setText(pwd);
        }
    }
}
